<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Find_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    //查询总数
    public function get_article_list_count($web_category=0){
        $this->db->select('article_id');
        $this->db->from('articles');
        if($web_category>0){
            $this->db->where('web_category',$web_category);
        }
        $this->db->where('article_lock',0);
        $total=$this->db->count_all_results();
        return $total;
    }

    /**
     * @param $id
     * @return mixed
     * 查询单条
     */
    public function get_articles($id){
        $query = $this->db
            ->select('*')
            ->get_where('articles', array('article_id'=>$id));
        return $query->row_array();
    }

    public function get_article_list($page=0,$limit=4,$aid=0,$web_category=1,$ord='desc',$id=0)
    {
        $start = $page*$limit;
        $this->db->select('*');
        $this->db->from('articles');
        if($web_category>0){
            $this->db->where('web_category',$web_category);
        }
        $this->db->where('article_lock',0);
        if($aid){
        $this->db->where('article_category',$aid);
        }
        $this->db->where('article_id <>',$id);
        $this->db->order_by('article_add_time',$ord);    
        $this->db->limit($limit,$start);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    //查询最新的一篇文章
    public function get_list_article($id=0){
        if($id){
            $query = $this->db->select('*')->from('articles')->order_by('article_id','desc')->where('article_category',$id)->where('web_category',99)->limit(1)->get();
        }else{
            $query = $this->db->select('*')->from('articles')->order_by('article_id','desc')->where('web_category',99)->limit(1)->get();
        }
        if($query->num_rows() > 0){
            return $query->row_array();
        }
    }
    public function count_articles_count_by_id($id=0){
        $this->db->select('article_id');
        if($id){
            $this->db->where('article_category',$id);
        }
        $this->db->where('web_category',99);
        $this->db->from('articles');
        
        $total=$this->db->count_all_results();
        return $total;
    }
   //查询这篇文章是否被收藏
   public function get_article_favorite($user_id,$article_id){
    $query = $this->db->select('*')->from('user_favorites')->where(array('item_type'=>1,'web_category'=>99,'user_id'=>$user_id,'item_id'=>$article_id))->get();
    if($query->num_rows() > 0){
        return $query->row_array();
    }
   }
   //删除文章的收藏
   public function del_article_favorite($user_id,$article_id){
        $this->db->where('item_id',$article_id)->where('user_id',$user_id)->where('item_type',1)->delete('user_favorites');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    public function avg_article_count_by_article_id($article_id){
        $this->db->select_avg('article_cover');
        $this->db->where('article_id',$article_id);
        // $this->db->where('order_status',2);
        $query = $this->db->get('articles');
        return $query->row_array();
    }

   }
